/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.app.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.app.annotation.Delay;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.annotation.LoginUser;
import io.renren.modules.app.dto.CreateOrderRespDto;
import io.renren.modules.app.entity.CustomerEntity;
import io.renren.modules.app.req.BalanceLogReq;
import io.renren.modules.app.req.BalanceRechargeReq;
import io.renren.modules.app.req.ForgetResetPasswordReq;
import io.renren.modules.app.service.BalanceLogService;
import io.renren.modules.app.service.BalanceOrderService;
import io.renren.modules.app.service.CustomerService;
import io.renren.modules.app.service.PublicService;
import io.renren.modules.app.utils.AuthUtils;
import io.renren.modules.app.vo.BalanceLogVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * APP用户接口
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/api/user")
@Api(tags = "APP用户接口")
public class UserController {

    @Resource
    private BalanceLogService balanceLogService;

    @Resource
    private CustomerService customerService;

    @Resource
    private BalanceOrderService balanceOrderService;

    @Delay(time = 5000)
    @Login
    @GetMapping("userInfo")
    @ApiOperation("获取用户信息")
    public R userInfo(@LoginUser CustomerEntity user){
        Map<String,Object> map = new HashMap<>();
        map.put("username",user.getUsername());
        map.put("balance",user.getBalance());
        return R.ok().put("data", map);
    }

    @Login
    @GetMapping("getBalanceLog")
    @ApiOperation("获取用户余额日志")
    public R getBalanceLog(Page<BalanceLogVO> page, BalanceLogReq balanceLogReq){
        PageUtils pages = balanceLogService.getBalanceLog(page,balanceLogReq);
        return R.ok().put("data", pages);
    }

    @Login
    @PostMapping("balanceRecharge")
    @ApiOperation("余额充值")
    public R balanceRecharge(@RequestBody @Valid BalanceRechargeReq balanceRechargeReq){
        //表单校验
        ValidatorUtils.validateEntity(balanceRechargeReq);
        CreateOrderRespDto createOrderRespDto = balanceOrderService.balanceRecharge(balanceRechargeReq);
        return R.ok().put("data",createOrderRespDto);
    }

    @ApiOperation("退款处理")
    @GetMapping("/refundCheck")
    public void refundCheck(){
        balanceOrderService.refundCheck();
    }

    @Login
    @GetMapping("userId")
    @ApiOperation("获取用户ID")
    public R userInfo(@RequestAttribute("userId") Integer userId){
        return R.ok().put("userId", userId);
    }

    @PostMapping("forgetResetPassword")
    @ApiOperation("忘记密码")
    public R forgetResetPassword(@RequestBody ForgetResetPasswordReq forgetResetPasswordReq){
        customerService.forgetResetPassword(forgetResetPasswordReq);
        return R.ok().put("msg", "修改成功");
    }

}
